package com.sunday.fangdaifu.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.fangdaifu.R;
import com.sunday.fangdaifu.common.ApiClient;
import com.sunday.fangdaifu.common.ApiServiceImpl;
import com.sunday.fangdaifu.model.Member;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by yun_sheng on 2017/6/29.
 */

public class SettingPwdActivity extends BaseActivity implements ApiServiceImpl.HttpResponseInterface{
    @Bind(R.id.edt_password)
    EditText edtPassword;
    @Bind(R.id.see)
    TextView see;
    @Bind(R.id.btn_confirm)
    TextView btnConfirm;
    private int userId;
    private boolean isSee = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acitivity_set_pwd);
        ButterKnife.bind(this);
        userId = getIntent().getIntExtra("userId",0);
    }
    @OnClick({R.id.btn_confirm,  R.id.see})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_confirm:
                showLoadingDialog();
                ApiClient.getApiService().settingPwd(userId,edtPassword.getText().toString(),this,new TypeToken<ResultDO<Member>>(){}.getType());
                break;
            case R.id.see:
                see.setSelected(!isSee);
                isSee = !isSee;
                if (isSee) {
                    edtPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    edtPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
                }

                break;
        }
    }

    @Override
    public void onResponce(String key, Object result) {
        if (isFinish){
            return;
        }
        dissMissDialog();
        ResultDO<Member> resultDO1 =(ResultDO<Member>)result;
        if (resultDO1.getCode() == 0) {
            Intent intent = new Intent(mContext, LoginActivity.class);
            startActivity(intent);
            finish();
        } else {
            ToastUtils.showToast(mContext, resultDO1.getMessage());
        }
    }

    @Override
    public void onFailure() {
        dissMissDialog();
        ToastUtils.showToast(mContext,"服务器异常");
    }
}
